package com.taihua_tech.basic.util;

import java.io.Serializable;
import java.security.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DataMap extends LinkedHashMap<String,Object> implements Serializable{
	private static final long serialVersionUID = -2612801519545002955L;
	
	public DataMap() {
	}
	public DataMap(Object... args) {
		put(args);
	}
	public boolean getBoolean(Object key){
		boolean result = false;
		String temp = String.valueOf(get(key));
		if ("true".equalsIgnoreCase(temp) || "yes".equalsIgnoreCase(temp)
				|| "y".equalsIgnoreCase(temp)){
			result = true;
		}
		return result;
	}
	public boolean getBoolean(Object key,boolean defultValue){
		boolean result = defultValue;
		String temp = String.valueOf(get(key));
		if ("true".equalsIgnoreCase(temp) || "yes".equalsIgnoreCase(temp)
				|| "y".equalsIgnoreCase(temp)){
			result = true;
		}
		return result;
	}
	public int getInt(Object key) {
		return getInt(key, 0);
	}
	public int getInt(Object key, int defaultInt) {
		Integer result = 0;
		Object temp = get(key);
		if (temp != null){
			result = Integer.parseInt(String.valueOf(temp));
		}
		return result == null ? defaultInt : result;
	}
	public String getString(Object key) {
		return (String) get(key);
	}
	public String stringValue(Object key) {
		Object value = get(key);
		String result = "";
		if (value != null){
			result = String.valueOf(value);
		}
		return result;
	}
	@SuppressWarnings("rawtypes")
	public List getList(Object key){
		List result = null;
		Object temp = get(key);
		if (temp instanceof List) {
			result = (List)temp;
		}
		return result;
	}
	public String getString(Object key, String defaultValue) {
		String value = (String) get(key);
		return value == null ? defaultValue : value;
	}
	public Timestamp getTimestamp(Object key) {
		return (Timestamp) get(key);
	}
	public void put(Object... args) {
		for (int i = 1; i < args.length; i += 2) {
			put(String.valueOf(args[i - 1]), args[i]);
		}
	}
	public DataMap put(String key, Object value) {
		if (key != null && value != null) {
			super.put(key, value);
		}
		return this;
	}
	
	@SuppressWarnings("rawtypes")
	public DataMap append(Map map) {
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			Object key = iter.next();
			if (this.containsKey(key))
				continue;
			Object value = map.get(key);
			this.put(key, value);
		}
		return this;
	}
	
	@SuppressWarnings("rawtypes")
	public DataMap update(Map map) {
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			Object key = iter.next();
			Object value = map.get(key);
			this.put(key, value);
		}
		return this;
	}
}
